
var $main_table = $("#main_table");

function get_table_columns() {
    return [
        {
            field: 'checkbox',
            checkbox: true
        },
        {
            field: 'cluster_id',
            title: '集群编号',
            filterControl: "input",
        },
        {
            field: 'cluster_name',
            title: '集群名称',
            filterControl: "input"
        },
        {
            field: 'cluster_type',
            title: '集群类型',
            filterControl: "select"
        },
        {
            field: 'cluster_status',
            title: '集群状态',
            filterControl: "select",
            formatter: function (value) {
                if (value == 0) {
                    return "待上线";
                } else if (value == 1) {
                    return "上线中";
                } else if (value == 2) {
                    return "维护中";
                } else if (value == 3) {
                    return "已下线";
                } else {
                    return "未知"
                }
            }
        },
        {
            field: 'create_time',
            title: '创建时间',
            filterControl: "input"
        },
        {
            field: 'operate',
            title: '操作',
            formatter: "toolsFormatter",
            visible: true
        },
    ];
}

function init_main_table() {
    $main_table.bootstrapTable('destroy');
    var pageSizes = getPageSize();
    let request_url = "/mysql_dbs/get_mysql_clusters/";
    let request_data = {};
    let resp_data = get_response_data(request_url, request_data);
    console.log(resp_data);
    var ex_option = {
        data: resp_data.data,
        toolbar: "#toolbar",
        columns: get_table_columns(),
    };
    var table_option = $.extend(true, get_table_basic_option(), ex_option);
    $main_table.bootstrapTable(table_option);
}

function toolsFormatter(value, row, index) {
    var detail_button = ' <a href="/mysql_ops/mysql_cluster_detail/?cluster_id=' + row.cluster_id + '\" data-toggle="tooltip" data-placement="bottom" title="详情">详情</a>';
    return detail_button;
}


function delete_rows(rows) {
    for (var row_index in rows) {
        let row = rows[row_index];
        console.log("准备删除记录: " + row.cluster_id.toString());
        // 调用rmove方法删除记录，当传入的值是INT类型时，需要将INT值转换成字符串。
        $main_table.bootstrapTable('remove', {
            field: 'cluster_id',
            values: row.cluster_id.toString()
        });
        console.log("成功删除记录:" + row.cluster_id.toString())
    }
}

function refresh_main_table() {
    console.log("refresh main table");
}

function btn_add() {
    show_main_modal({}, refresh_main_table)
}

function btn_edit() {
    var rows = $main_table.bootstrapTable('getSelections');
    if ((rows.length == 0) || (rows.length > 1)) {
        show_alter("操作确认", "每次只能修改一条记录，请确认要修改的记录。");
        return;
    }
    console.log("您选择修改记录：", rows[0].cluster_id.toString());
    show_main_modal(rows[0], refresh_main_table);
    // window.location.href = "/assets/server_edit/?server_id=" + rows[0].server_id
}

function btn_delete() {
    var rows = $main_table.bootstrapTable('getSelections');
    let confirm_data = { 'rows': rows };
    if (rows.length == 0) {
        show_alter("操作确认", "请选择您要删除的记录。");
        return;
    }
    // 注意show_confirm方法参数：title,message,confirm_action=null, cancel_action=null,confirm_data=null
    show_confirm(
        "操作确认",
        "请确认要删除选中的" + rows.length.toString() + "条记录么?",
        function (confirm_data) {
            delete_rows(confirm_data['rows']);
        },
        function (confirm_data) {
            console.log('user cancal...');
        },
        confirm_data
    );
}

function getHeight() {
    return $(window).height() - 160;
}

function getPageSize() {
    var totalHeight = $(window).height() - 160;
    return Math.floor(Number(totalHeight) / 45)
}

$(document).ready(function () {
    init_main_table();
    $('#toobarSelect').change(function () {
        $main_table.bootstrapTable('refreshOptions', {
            exportDataType: $(this).val()
        });
    });
})


//======================modal script====================================//
var $main_modal_form = $("#main_modal_form");
var $main_modal = $('#main_modal');
var $call_back_method = null;
var $modal_post_url = "/mysql_ghost/add_ghost_job/"
var $modal_title = "GHOST作业"

function init_main_modal_button() {
    $("#btn_modal_cancel").unbind("click");
    $("#btn_modal_submit").unbind("click");
    $("#btn_modal_cancel").bind("click", function () {
        // $main_modal_form.bootstrapValidator("resetForm", true);
    });
    $("#btn_modal_submit").bind("click", function () {
        submit_main_modal_form();
    })
}

function submit_main_modal_form() {
    var $modal_data = get_modal_data($main_modal);
    $.post($modal_post_url, $modal_data, function (data) {
        if (data.return_code == 1) {
            show_alert_text("操作信息", data.return_message);
        } else {
            show_alert_text("操作信息", data.return_message);
        }
        $main_modal.modal('hide');
        clear_form($main_modal_form);
        $call_back_method();
    });
}


function init_main_modal(modal_data) {
    clear_form($main_modal_form);
    console.log(modal_data);
    fill_modal_data($main_modal, modal_data);
}


function show_main_modal(modal_data, call_back_method) {
    $call_back_method = call_back_method;
    init_main_modal_button();
    init_main_modal(modal_data);
    $main_modal.find('.modal-title').text($modal_title);
    $main_modal.modal("show");
}
//======================modal script====================================//